package com.task.mall.controller.client;

import com.google.gson.Gson;
import com.task.mall.bean.*;
import com.task.mall.service.GoodsService;
import com.task.mall.service.Msgservice;
import com.task.mall.service.Orderservice;
import com.task.mall.service.TypeService;
import com.task.mall.service.impl.GoodsServiceImpl;
import com.task.mall.service.impl.MsgServiceImpl;
import com.task.mall.service.impl.OrderserviceImpl;
import com.task.mall.service.impl.TypeServiceImpl;
import com.task.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/client/mall/*")
public class MallServlet extends HttpServlet {
    GoodsService goodsService = new GoodsServiceImpl();
    TypeService typeService = new TypeServiceImpl();
    Orderservice orderservice = new OrderserviceImpl();
    Msgservice msgService = new MsgServiceImpl();

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/client/mall/", "");
        if ("askGoodsMsg".equals(action)){
            askGoodsMsg(request,response);
        }else if ("addOrder".equals(action)){
            addOrder(request,response);
        }else if("sendComment".equals(action)){
            sendComment(request,response);
        } else if("settleAccounts".equals(action)){
            settleAccounts(request,response);
        }
    }

    /**
     * 一键下单功能 -- 核心即修改对应的订单号的stateId即可
     * @param request
     * @param response
     */
    private void settleAccounts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Cart cart = gson.fromJson(requestBody, Cart.class);
        int result = orderservice.settleAccounts(cart);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 发起你的评论
     * @param request
     * @param response
     */
    private void sendComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Comment comment = gson.fromJson(requestBody, Comment.class);
        int result = orderservice.sendComment(comment);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 立即购买按钮 -- 自动付款成功 -- 等待派送
     * @param request
     * @param response
     */
    private void addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AddOrder order = gson.fromJson(requestBody, AddOrder.class);
        int result = orderservice.AddOrder(order);
        Result res = new Result();
        if (result == 200){
             res.setCode(0);
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 发起质问
     * @param request
     * @param response
     */
    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Question question = gson.fromJson(requestBody, Question.class);
        int result = msgService.askGoodsMsg(question);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        }
        response.getWriter().println(gson.toJson(res));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/client/mall/", "");
        if ("getType".equals(action)){
            getType(request,response);
        }else if ("getGoodsByType".equals(action)){
            getGoodsByType(request,response);
        }else if ("searchGoods".equals(action)){
            searchGoods(request,response);
        }else if ("getGoodsInfo".equals(action)){
            getGoodsInfo(request,response);
        }else if ("getGoodsMsg".equals(action)){
            getGoodsMsg(request,response);
        }else if("getOrderByState".equals(action)){
            getOrderByState(request,response);
        }else if ("getGoodsComment".equals(action)){
            getGoodsComment(request,response);
        }else if ("deleteOrder".equals(action)){
            deleteOrder(request,response);
        }else if ("confirmReceive".equals(action)){
            confirmReceive(request,response);
        }else if ("pay".equals(action)){
            pay(request,response);
        }
    }

    /**
     * 确认付款
     * @param request
     * @param response
     */
    private void pay(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int result = orderservice.pay(id);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 用户确认收到货
     * @param request
     * @param response
     */
    private void confirmReceive(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int result = orderservice.confirmReceive(id);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 用户个人删除订单
     * @param request
     * @param response
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int result = orderservice.userDeleteOrder(id);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 获取历史用户对商品的评价信息
     * @param request
     * @param response
     */
    private void getGoodsComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String goodsId = request.getParameter("goodsId");
        Rate rate = msgService.getGoodsComment(goodsId);
        Result res = new Result();
        res.setCode(0);
        res.setData(rate);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 获取不同状态的订单信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void getOrderByState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String token = request.getParameter("token");
        String state = request.getParameter("state");
        List<OrderByState> orderByState = orderservice.getOrderByState(token, state);
        Result res = new Result();
        res.setCode(0);
        res.setData(orderByState);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 获取该商品的问答
     * @param request
     * @param response
     */
    private void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        List<ShowMsg> showMsg = goodsService.showMsg(id);
        Result res = new Result();
        res.setCode(0);
        res.setData(showMsg);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 点击商品后获得相关商品详情
     * @param request
     * @param response
     */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        GoodsDetails goodsDetails = goodsService.goodsDetails(id);
        Result res = new Result();
        res.setCode(0);
        res.setData(goodsDetails);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 输入关键字搜索相关商品
     * @param request
     * @param response
     */
    private void searchGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String keyword = request.getParameter("keyword");
        List<Goods> goodsList = typeService.searchGoods(keyword);
        Result res = new Result();
        res.setCode(0);
        res.setData(goodsList);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 获得类目
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = typeService.getType();
        Result res = new Result();
        res.setCode(0);
        res.setData(typeList);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 获得不同类目的商品
     * @param request
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        List<Goods> goodsList = typeService.getGoodsByType(typeId);
        Result res = new Result();
        res.setCode(0);
        res.setData(goodsList);
        response.getWriter().println(gson.toJson(res));
    }
}
